﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chbl.DAL;
using Chbl.Models;

namespace Chbl.Controllers.AdminRole
{
    [Authorize(Roles = "admin")]
    public class AdminDivisionController : Controller
    {
        private readonly ChblDb _db;

        public AdminDivisionController(ChblDb db)
        {
            _db = db;
        }

        //
        // GET: /AdminDivision/        
        public ViewResult Index()
        {
            return View(_db.Divisions.ToList());
        }

        //
        // GET: /AdminDivision/Details/5

        public ViewResult Details(int id)
        {
            Division division = _db.Divisions.Find(id);
            return View(division);
        }

        //
        // GET: /AdminDivision/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /AdminDivision/Create

        [HttpPost]
        public ActionResult Create(Division division)
        {
            if (ModelState.IsValid)
            {
                _db.Divisions.Add(division);
                _db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(division);
        }
        
        //
        // GET: /AdminDivision/Edit/5
 
        public ActionResult Edit(int id)
        {
            Division division = _db.Divisions.Find(id);
            return View(division);
        }

        //
        // POST: /AdminDivision/Edit/5

        [HttpPost]
        public ActionResult Edit(Division division)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(division).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(division);
        }

        //
        // GET: /AdminDivision/Delete/5
 
        public ActionResult Delete(int id)
        {
            Division division = _db.Divisions.Find(id);
            return View(division);
        }

        //
        // POST: /AdminDivision/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Division division = _db.Divisions.Find(id);
            _db.Divisions.Remove(division);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}